function calculate(expression) {
    const regex = /^\s*(-?\d+\.?\d*)\s*([+\-*/])\s*(-?\d+\.?\d*)\s*$/;
    const match = expression.match(regex);
    if (!match) {
      return '错误：表达式格式错误或操作符错误';
    }
    const num1 = parseFloat(match[1]);
    const operator = match[2];
    const num2 = parseFloat(match[3]);
    
    if (operator === '/' && num2 === 0) {
      return '错误：除数不能为零';
    }
    
    let result;
    switch(operator) {
      case '+':
        result = num1 + num2;
        break;
      case '-':
        result = num1 - num2;
        break;
      case '*':
        result = num1 * num2;
        break;
      case '/':
        result = num1 / num2;
        break;
      default:
        return '错误：未知的运算符';
    }
    
    return result;
  }
  
  // 添加更多测试用例
  const tests = [
    { expr: '1+2', expected: 3 },
    { expr: '3-2', expected: 1 },
    { expr: '2 * 3', expected: 6 },
    { expr: '4/2', expected: 2 },
    { expr: '4/0', expected: '错误：除数不能为零' },
    { expr: '1++2', expected: '错误：表达式格式错误或操作符错误' },
    { expr: 'abc+3', expected: '错误：表达式格式错误或操作符错误' },
    { expr: '5%3', expected: '错误：表达式格式错误或操作符错误' },
    { expr: '3.5 * 2', expected: 7 },
    { expr: '-5 * 4', expected: -20 },
    { expr: '2.5 / 0.5', expected: 5 },
    { expr: '  6   /   3  ', expected: 2 },
    { expr: '10 + 20', expected: 30 },
    { expr: '100 - 50', expected: 50 },
    { expr: '12 * 12', expected: 144 },
    { expr: '100 / 10', expected: 10 },
    { expr: 'abc + def', expected: '错误：表达式格式错误或操作符错误' },
    { expr: '1 + 2 + 3', expected: '错误：表达式格式错误或操作符错误' },
  ];
  
  tests.forEach(test => {
    const result = calculate(test.expr);
    console.log(`测试表达式：${test.expr}`);
    console.log(`预期结果：${test.expected}，实际结果：${result}`);
    console.log('---');
  });